<?php
namespace app\admin\controller;
use app\parent\admin;
use think\Db;

class File extends admin
{

    // 文件列表
    public function file($table = 'admin_file')
    {
        if(!$this->isAjax()){
            $data['info'] = $this->returnType();
            $data['yuming'] = get_default('yun_t_yuming');
            return view('file',$data);
        }

        $where = $this->whereSql([
            'like' => ['admin_file_p_title'],
            'time' => ['admin_file_times'],
            'eq' => ['admin_file_is_show'],
        ]);
        
        $pid = $this->input('admin_file_p_id',0);
        
        $pa = $this->input(['page','limit'],1);
        $page = ($pa['page'] - 1) * $pa['limit'];

        $data = Db::name($table)
            ->where($where)
            ->where('admin_file_p_id',$pid)
            ->order('admin_file_file asc,id desc')
            ->limit($page,$pa['limit'])
            // ->fetchSql(true)
            ->select();
        
        $count = Db::name($table)
            ->where($where)
            ->where('admin_file_p_id',$pid)
            ->count('id');

        return $this->ajaxReturn(0,'',$data,$count);
        
    }

    // 文件列表
    public function filelist($table = 'admin_file')
    {
        if(!$this->isAjax()){
            $data = $this->input(['key','name','number']);
            $data['info'] = $this->returnType();
            $data['yuming'] = get_default('yun_t_yuming');
            return view('filelist',$data);
        }

        $where = $this->whereSql([
            'like' => ['admin_file_p_title'],
            'time' => ['admin_file_times'],
            'eq' => ['admin_file_is_show'],
        ]);
        
        $pid = $this->input('admin_file_p_id',0);
        
        $pa = $this->input(['page','limit'],1);
        $page = ($pa['page'] - 1) * $pa['limit'];

        $data = Db::name($table)
            ->where($where)
            ->where('admin_file_p_id',$pid)
            ->where('admin_file_file',1)
            ->order('id desc')
            ->limit($page,$pa['limit'])
            // ->fetchSql(true)
            ->select();
        
        $count = Db::name($table)
            ->where($where)
            ->where('admin_file_p_id',$pid)
            ->where('admin_file_file',1)
            ->count('id');

        return $this->ajaxReturn(0,'',$data,$count);
        
    }

    public function returnType($pid = 0,$arr = [],$str = '|------')
    {
        $data = Db::name('admin_file')->where('admin_file_p_id',$pid)->select();
        if($data){
            foreach($data as $k => $v){
                $v['admin_file_p_title'] = $str . $v['admin_file_p_title'];
                $arr[] = $v;
                $arr = $this->returnType($v['id'],$arr,$str.'|------');
            }
        }

        return $arr;
    }

    public function fileAdd($table = 'admin_file')
    {

        if (!$this->isPost()) {
            $viewArr['info'] = $this->returnType();
            return view('fileAdd',$viewArr);
        }
        ini_set('memory_limit', '1024M');
        $data = $this->input(['admin_file_p_id','admin_file_p_title','admin_file_file','admin_file_is_show']);
        $info = $this->input(['pic','zoom','pictype']);
        $pictype = explode('.',$info['pictype']);
        
        if($data['admin_file_file'] == 1){
            $file = $this->base64_file($info['pic'],$info['zoom'],$pictype[1]);

            $data['admin_file_w'] = $file['admin_file_w'];
            $data['admin_file_h'] = $file['admin_file_h'];
            $data['admin_file_type'] = $file['admin_file_type'];

            $cosClient = new \Qcloud\Cos\Client(array('region' => get_default('yun_t_diqu'),'credentials'=> array(
                'secretId'    => get_default('yun_t_appid'),
                'secretKey' =>  get_default('yun_t_key'))));
            $result = $cosClient->Upload(
                get_default('yun_t_shuitong'),
                $file['key'],
                fopen('.'.$file['admin_file_url'], 'rb'));
            unlink('.'.$file['admin_file_url']);
            $data['admin_file_url'] = $file['key'];
        }else{
            $data['admin_file_url'] = '/web/images/a/file.png';
        }

        $data['admin_file_times'] = time();
        $is = Db::name($table)->insert($data);

        if($is){
            return $this->ajaxReturn(0,'操作成功');
        }else{
            return $this->ajaxReturn(1,'操作失败');
        }
        
    }

    public function fileDel($table = 'admin_file')
    {
        $id = $this->input('id',0); // 获取id
        
        $is = Db::name($table)->where('id','in',$id)->delete();

        if($is){
            return $this->ajaxReturn(0,'操作成功');
        }else{
            return $this->ajaxReturn(1,'操作失败');
        }
        
    }

    /**
     *  保存base64图片到图片库
     *  $name = 图片名称
     *  $str  = 不存在请求参数下标
     *  $arr  = 如果多张是否返回数组
     */
    public function base64_file($pic,$zoom = 1,$type)
    {
        $file_str = '/web/images/file/';
        $name_file = date('YmdHis').$this->session('adminid');

        if(!file_exists(".".$file_str)){
            mkdir(".".$file_str);
        }

        $pic = explode(',',$pic);
        $pic_file = $pic[count($pic)-1];
        $pic_str = $file_str.$name_file.'.'.$type;
        file_put_contents('.'.$pic_str,base64_decode($pic_file));

        if(file_exists(".".$pic_str) && is_file(".".$pic_str)){
            $image = \think\Image::open('.'.$pic_str);
            // 返回图片的宽度
            $arr['admin_file_w'] = $image->width(); 
            // 返回图片的高度
            $arr['admin_file_h'] = $image->height(); 
            // 返回图片的类型
            $arr['admin_file_type'] = $image->type();

            $image->save('.'.$pic_str,$arr['admin_file_type'],$zoom);
            $arr['admin_file_url'] = $pic_str;
            $arr['key'] = $name_file.'.'.$type;
        }else{
            $arr['admin_file_url'] = '';
            $arr['admin_file_w'] = '';
            $arr['admin_file_h'] = '';
            $arr['admin_file_type'] = '';
            $arr['key'] = '';
        }
           
        return $arr;
    }

   public function filefile($table = 'agency_file')
    {
        
        //上传配置
        $config = array(
            "savePath" => "web/images/file" ,             //存储文件夹
            "maxSize" => 1000 ,                   //允许的文件最大尺寸，单位KB
            "allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" )  //允许的文件格式
        );
        //上传文件目录

        //背景保存在临时目录中
        $up = new \app\classs\Uploader( "upfile" , $config );

        $info = $up->getFileInfo();
        echo json_encode($info['json']);
        
        $data = $info['data'];

        $pid = Db::name('agency_file_type')
            ->where('agency_file_type_w_id',0)
            ->where('agency_file_type_p_id',0)
            ->where('agency_file_type_type',3)
            ->order('agency_file_type_sort asc')
            ->value('id');

        $data['agency_file_w_id'] = $this->session('wid');
        $data['agency_file_p_id'] = $pid;
        $data['agency_file_times'] = time();
        Db::name($table)->insert($data);
        
        die;

    }
    
}
